﻿using System;
using System.Linq;
using Microsoft.Practices.SharePoint.Common.Configuration;
using Microsoft.SharePoint.Administration;
using SharePoint.Contrib.App.Settings;
using SharePoint.Contrib.Diagnostics;

namespace SharePoint.Contrib.Extensions
{
    /// <summary>
    /// Extensions for the SPFarm class
    /// </summary>
    public static class SPFarmExtensions
    {
        /// <summary>
        /// Retrieves a web application from the SPfarm
        /// </summary>
        /// <param name="id">The id of the web application</param>
        public static SPWebApplication WebApplication(this SPFarm instance, Guid id)
        {
            return instance
                        .Services
                        .GetValue<SPWebService>()
                        .WebApplications[id];
        }

        /// <summary>
        /// Retrieves the collection of settings for the SPFarm
        /// </summary>
        public static ISettingCollection Settings(this SPFarm instance)
        {
            IPropertyBag properties = instance.PropertiesBag();
            return new SPFarmSettingCollection(properties, instance);
        }

        private static IPropertyBag PropertiesBag(this SPFarm instance)
        {
            IPropertyBag properties = new SPFarmPropertyBag(instance);
            return properties;
        }
    }
}
